package com.rjwh.dingdong.client.activity.radio;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.aishua.lib.utils.AsStrUtils;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.player.fragment.PlayerToolbarFragment;
import com.rjwh.dingdong.client.adapter.RadioAlbumListAdapter;
import com.rjwh.dingdong.client.bean.localbean.RadioPageAlbumData;
import com.rjwh.dingdong.client.mvp.presenter.impl.RadioAlbumListPresenter;
import com.rjwh.dingdong.client.mvp.view.RadioAlbumListView;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.view.MySwipyRefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author Kay
 * @ClassName RadioAlbumListActivity
 * @Description 专辑列表(首发, 推荐;儿歌)
 * @date 2016-11-15
 * @history 2016-11-15 author: description:
 */
public class RadioAlbumListActivity extends BaseActivity implements RadioAlbumListView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.radio_album_list_lv)
    ListView radioAlbumListLv;
    @BindView(R.id.radio_album_list_swipyRefreshView)
    MySwipyRefreshLayout radioAlbumListSwipyRefreshView;

    private RadioAlbumListPresenter presenter;
    private RadioAlbumListAdapter recommendedAlbumListAdapter;
    private int pageNumber = 0;
    private String category;
    private String categoryName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio_album_list_layout);
        ButterKnife.bind(this);
        parseIntent();
        initView();
        presenter = new RadioAlbumListPresenter(this, this);
        radioAlbumListSwipyRefreshView.doPullRefreshing();
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            category = intent.getStringExtra("category");
            categoryName = intent.getStringExtra("categoryName");
        }
    }

    private void initView() {
        initToolbarHelper();
        getSupportFragmentManager().beginTransaction().replace(R.id.radio_album_list_fm, PlayerToolbarFragment.getInstance()).commit();

        recommendedAlbumListAdapter = new RadioAlbumListAdapter(this, false);
        radioAlbumListLv.setAdapter(recommendedAlbumListAdapter);
        radioAlbumListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                RadioAlbumListAdapter adapter = (RadioAlbumListAdapter) parent.getAdapter();
                RadioPageAlbumData item = adapter.getItem(position);
                String actionurl = item.getActionurl();
                String albumname = item.getAlbumname();
                if (!AsStrUtils.isEmpty(actionurl)) {
                    Intent intent = UrlUtil.handelUrl(RadioAlbumListActivity.this, actionurl, false);
                    if (intent == null) {
                        return;
                    }
                    intent.putExtra("albumname", albumname);
                    startActivity(intent);
                }
            }
        });

        radioAlbumListSwipyRefreshView.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                switch (direction) {
                    case TOP:
                        pageNumber = 0;
                        doGetRadioAlbumList(true, SwipyRefreshLayoutDirection.TOP);
                        break;

                    case BOTTOM:
                        pageNumber++;
                        doGetRadioAlbumList(false, SwipyRefreshLayoutDirection.BOTTOM);
                        break;

                    default:
                        break;
                }
            }
        });
    }

    protected void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        assert getSupportActionBar() != null;
        toolbar.setTitle("");
        getSupportActionBar().setTitle(categoryName);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void doGetRadioAlbumList(boolean isRefresh, SwipyRefreshLayoutDirection direction) {
        presenter.doGetRadioAlbumList(category, pageNumber, isRefresh, direction);
    }

    @Override
    public void setDirection(SwipyRefreshLayoutDirection direction) {
        radioAlbumListSwipyRefreshView.setDirection(direction);
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        radioAlbumListSwipyRefreshView.setRefreshing(isRefresh);
    }

    @Override
    public void loadData(boolean isRefresh, List<RadioPageAlbumData> albumlist) {
        if (isRefresh) {
            recommendedAlbumListAdapter.clearAll();
        }
        recommendedAlbumListAdapter.appendToList(albumlist);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.detachView();
    }
}
